package com.tycmc.signelectroniccontracts.tools.store;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;

import com.tycmc.signelectroniccontracts.entity.PDFPositionEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gy on 2017/9/25.
 */

public class DBHelper {
    public static void init(Context context) {
        SqlitTool.getDatabase(context);
    }

    public static long save(Context context, PDFPositionEntity entity) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("position", entity.getPosition());
        contentValues.put("urlpath", entity.getUrlpath());
        contentValues.put("ordernum", entity.getOrdernum());
        long insert;
        insert = SqlitTool.getDatabase(context).insert(SqlitTool.TABLE_PDF, null, contentValues);
        return insert;
    }




    public static long delete(Context context, String position) {
        if (!TextUtils.isEmpty(position)) {
            return SqlitTool.getDatabase(context).delete(SqlitTool.TABLE_PDF, "position=?", new String[]{position});
        }
        return 0;
    }

    public static List<PDFPositionEntity> quary(Context context, String position) {
        if (TextUtils.isEmpty(position)) {
            return null;
        }
        Cursor query = SqlitTool.getDatabase(context).query(SqlitTool.TABLE_PDF, null, "position = ?", new String[]{position}, null, null, "ordernum ASC");

        List<PDFPositionEntity> list = new ArrayList<>();
        while (query.moveToNext()) {
            PDFPositionEntity entity = new PDFPositionEntity();
            entity.setPosition(query.getString(query.getColumnIndex("position")));
            entity.setUrlpath(query.getString(query.getColumnIndex("urlpath")));
            entity.setOrdernum(query.getInt(query.getColumnIndex("ordernum")));
            list.add(entity);
        }
        return list;
    }
}
